home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gdevnp6.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  8KB  |  259 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevnp6.c */
  20. /* NEC dot-matrix printer driver for Ghostscript: */
  21. /* tested on P6, should also work on P6+ and similar models. */
  22. #include "gdevprn.h"
  23.  
  24. /* Thanks to Andreas Schwab (schwab@ls5.informatik.uni-dortmund.de) */
  25. /* for improvements to this code. */
  26.  
  27. /* The device descriptors */
  28. private dev_proc_print_page (necp6_print_page);
  29. gx_device_printer gs_necp6_device
  30.   = prn_device (prn_std_procs, "necp6",
  31.         85,        /* width_10ths, 8.5" */
  32.         110,        /* height_10ths, 11" */
  33.         360, 360,
  34.         0, 0, 0.5, 0,    /* margins */
  35.         1, necp6_print_page);
  36.  
  37. /* ------ Internal routines ------ */
  38.  
  39. /* Forward references */
  40. private void necp6_output_run (P4 (byte *, int, int, FILE *));
  41. private void necp6_improve_bitmap (P2 (byte *, int));
  42.  
  43. /* Send the page to the printer. */
  44. private int
  45. necp6_print_page (gx_device_printer *pdev, FILE *prn_stream)
  46. {
  47.   int xres = pdev->x_pixels_per_inch;
  48.   int yres = pdev->y_pixels_per_inch;
  49.   int x_high = (xres == 360);
  50.   int y_high = (yres == 360);
  51.   int bits_per_column = (y_high ? 48 : 24);
  52.   uint line_size = gdev_prn_raster (pdev);
  53.   uint in_size = line_size * bits_per_column;
  54.   byte *in = (byte *) gs_malloc (in_size, 1, "necp6_print_page (in)");
  55.   uint out_size = ((pdev->width + 7) & -8) * 3;
  56.   byte *out = (byte *) gs_malloc (out_size, 1, "necp6_print_page (out)");
  57.   int y_passes = (y_high ? 2 : 1);
  58.   int dots_per_space = xres / 10;    /* pica space = 1/10" */
  59.   int bytes_per_space = dots_per_space * 3;
  60.   int skip = 0, lnum = 0, ypass;
  61.  
  62.   /* Check allocations */
  63.   if (in == 0 || out == 0)
  64.     {
  65.       if (out)
  66.     gs_free ((char *) out, out_size, 1, "necp6_print_page (out)");
  67.       if (in)
  68.     gs_free ((char *) in, in_size, 1, "necp6_print_page (in)");
  69.       return_error (gs_error_VMerror);
  70.     }
  71.  
  72.   /* Initialize the printer and reset the margins. */
  73. #define init_string "\033@\033P\033l\000\r\034\063\001\033Q"
  74.   fwrite (init_string, sizeof (init_string) - 1, sizeof (char), prn_stream);
  75.   fputc ((int) (pdev->width / pdev->x_pixels_per_inch * 10) + 2,
  76.      prn_stream);
  77.  
  78.   /* Print lines of graphics */
  79.   while (lnum < pdev->height)
  80.     {
  81.       byte *inp;
  82.       byte *in_end;
  83.       byte *out_end;
  84.       byte *out_blk;
  85.       register byte *outp;
  86.       int lcnt;
  87.  
  88.       /* Copy 1 scan line and test for all zero. */
  89.       gdev_prn_copy_scan_lines (pdev, lnum, in, line_size);
  90.       if (in[0] == 0
  91.       && !memcmp ((char *) in, (char *) in + 1, line_size - 1))
  92.     {
  93.       lnum++;
  94.       skip += 2 - y_high;
  95.       continue;
  96.     }
  97.  
  98.       /* Vertical tab to the appropriate position. */
  99.       while ((skip >> 1) > 255)
  100.     {
  101.       fputs ("\033J\377", prn_stream);
  102.       skip -= 255 * 2;
  103.     }
  104.  
  105.       if (skip)
  106.     {
  107.       if (skip >> 1)
  108.         fprintf (prn_stream, "\033J%c", skip >> 1);
  109.       if (skip & 1)
  110.         fputc ('\n', prn_stream);
  111.     }
  112.  
  113.       /* Copy the rest of the scan lines. */
  114.       if (y_high)
  115.     {
  116.       inp = in + line_size;
  117.       for (lcnt = 1; lcnt < 24; lcnt++, inp += line_size)
  118.         if (!gdev_prn_copy_scan_lines (pdev, lnum + lcnt * 2, inp,
  119.                        line_size))
  120.           {
  121.         memset (inp, 0, (24 - lcnt) * line_size);
  122.         break;
  123.           }
  124.       inp = in + line_size * 24;
  125.       for (lcnt = 0; lcnt < 24; lcnt++, inp += line_size)
  126.         if (!gdev_prn_copy_scan_lines (pdev, lnum + lcnt * 2 + 1, inp,
  127.                        line_size))
  128.           {
  129.         memset (inp, 0, (24 - lcnt) * line_size);
  130.         break;
  131.           }
  132.     }
  133.       else
  134.     {
  135.       lcnt = 1 + gdev_prn_copy_scan_lines (pdev, lnum + 1, in + line_size,
  136.                            in_size - line_size);
  137.       if (lcnt < 24)
  138.         /* Pad with lines of zeros. */
  139.         memset (in + lcnt * line_size, 0, in_size - lcnt * line_size);
  140.     }
  141.  
  142.       for (ypass = 0; ypass < y_passes; ypass++)
  143.     {
  144.       out_end = out;
  145.       inp = in;
  146.       if (ypass)
  147.         inp += line_size * 24;
  148.       in_end = inp + line_size;
  149.  
  150.       for (; inp < in_end; inp++, out_end += 24)
  151.         {
  152.           memflip8x8 (inp, line_size, out_end, 3);
  153.           memflip8x8 (inp + line_size * 8, line_size, out_end + 1, 3);
  154.           memflip8x8 (inp + line_size * 16, line_size, out_end + 2, 3);
  155.         }
  156.       /* Remove trailing 0s. */
  157.       while (out_end - 3 >= out && out_end[-1] == 0
  158.          && out_end[-2] == 0 && out_end[-3] == 0)
  159.         out_end -= 3;
  160.  
  161.       for (out_blk = outp = out; outp < out_end;)
  162.         {
  163.           /* Skip a run of leading 0s. */
  164.           /* At least 10 are needed to make tabbing worth it. */
  165.  
  166.           if (outp[0] == 0 && outp + 12 <= out_end
  167.           && outp[1] == 0 && outp[2] == 0
  168.           && outp[3] == 0 && outp[4] == 0 && outp[5] == 0
  169.           && outp[6] == 0 && outp[7] == 0 && outp[8] == 0
  170.           && outp[9] == 0 && outp[10] == 0 && outp[11] == 0)
  171.         {
  172.           byte *zp = outp;
  173.           int tpos;
  174.           byte *newp;
  175.           outp += 12;
  176.           while (outp + 3 <= out_end
  177.              && outp[0] == 0 && outp[1] == 0 && outp[2] == 0)
  178.             outp += 3;
  179.           tpos = (outp - out) / bytes_per_space;
  180.           newp = out + tpos * bytes_per_space;
  181.           if (newp > zp + 10)
  182.             {
  183.               /* Output preceding bit data. */
  184.               /* only false at beginning of line */
  185.               if (zp > out_blk)
  186.             {
  187.               if (x_high)
  188.                 necp6_improve_bitmap (out_blk, (int) (zp - out_blk));
  189.               necp6_output_run (out_blk, (int) (zp - out_blk),
  190.                       x_high, prn_stream);
  191.             }
  192.               /* Tab over to the appropriate position. */
  193.               fprintf (prn_stream, "\033D%c%c\t", tpos, 0);
  194.               out_blk = outp = newp;
  195.             }
  196.         }
  197.           else
  198.         outp += 3;
  199.         }
  200.       if (outp > out_blk)
  201.         {
  202.           if (x_high)
  203.         necp6_improve_bitmap (out_blk, (int) (outp - out_blk));
  204.           necp6_output_run (out_blk, (int) (outp - out_blk), x_high,
  205.                   prn_stream);
  206.         }
  207.  
  208.       fputc ('\r', prn_stream);
  209.       if (ypass < y_passes - 1)
  210.         fputc ('\n', prn_stream);
  211.     }
  212.       skip = 48 - y_high;
  213.       lnum += bits_per_column;
  214.     }
  215.  
  216.   /* Eject the page and reinitialize the printer */
  217.   fputs ("\f\033@", prn_stream);
  218.   fflush (prn_stream);
  219.  
  220.   gs_free ((char *) out, out_size, 1, "necp6_print_page (out)");
  221.   gs_free ((char *) in, in_size, 1, "necp6_print_page (in)");
  222.  
  223.   return 0;
  224. }
  225.  
  226. /* Output a single graphics command. */
  227. private void
  228. necp6_output_run (byte *data, int count, int x_high, FILE *prn_stream)
  229. {
  230.   int xcount = count / 3;
  231.   fputc (033, prn_stream);
  232.   fputc ('*', prn_stream);
  233.   fputc ((x_high ? 40 : 39), prn_stream);
  234.   fputc (xcount & 0xff, prn_stream);
  235.   fputc (xcount >> 8, prn_stream);
  236.   fwrite (data, 1, count, prn_stream);
  237. }
  238.  
  239. /* If xdpi == 360, the NEC P6 cannot print adjacent pixels.  Clear the
  240.    second last pixel of every run of set pixels, so that the last pixel
  241.    is always printed.  */
  242. private void
  243. necp6_improve_bitmap (byte *data, int count)
  244. {
  245.   int i;
  246.   register byte *p = data + 6;
  247.  
  248.       for (i = 6; i < count; i += 3, p += 3)
  249.     {
  250.       p[-6] &= ~(~p[0] & p[-3]);
  251.       p[-5] &= ~(~p[1] & p[-2]);
  252.       p[-4] &= ~(~p[2] & p[-1]);
  253.     }
  254.       p[-6] &= ~p[-3];
  255.       p[-5] &= ~p[-2];
  256.       p[-4] &= ~p[-1];
  257.  
  258. }
  259.